<?php
/**
 * Created by PhpStorm.
 * User: yulin
 * Date: 2018-7-27
 * Time: 12:09
 */
namespace app\open\validate;

use app\base\validate\BaseValidate;

class DepartmentValidate extends BaseValidate
{
    protected $rule = [
        'fields'    => 'checkField',
        'where_json'=> 'checkJson',
        'page'      => 'number',
        'pagenum'   => 'number',
        'counts'    => 'number',
        'company_id'=> 'number',
        'parent_id' => 'number',


        'id' => 'require|checkIDs',
    ];

    protected $message = [
        'where_json.checkJson'=>'必须为json字符串'
    ];

    protected $default = [
        'page'   =>1,
        'pagenum'=>10,
        'fields' =>'*',
        'counts' =>9999999,
        'parent_id' => 0
    ];

    public $scene = [
        'list'=>[
            'name',
            'company_id',
            'parent_id'
        ],
        'add'=>[
            'name',
            'company_id',
            'parent_id'
        ],
        'edit'=>[
            'department_id',
            'name',
            'company_id',
            'parent_id'
        ],
        'delete'=>[
            'department_id',
        ],
    ];

    public function checkField($value){
        if($value=='*') return true;
        return true;
    }

    public function checkJson($value){
        if($value == null) return true;
        if(is_array(json_decode($value,true))) return true;
        return false;
    }
}